<template>
  <div class="page">
    <div class="searchline" style="margin: 20px 0">
      <el-input
        placeholder="职位名称"
        v-model="search.Job_name"
        size="medium"
        style="width: 200px; margin-left: 12px"
      ></el-input>
      <el-input
        placeholder="投递人"
        v-model="search.user_name"
        size="medium"
        style="width: 200px; margin-left: 12px"
      ></el-input>
      <el-select
        placeholder="状态"
        size="medium"
        v-model="search.status"
        style="width: 200px; margin-left: 12px"
      >
        <el-option label="已回复" :value="1"> </el-option>
        <el-option label="未回复" :value="0"> </el-option>
      </el-select>
      <el-button
        style="margin-left: 12px"
        size="medium"
        type="primary"
        @click="searchFromList(search)"
        >筛选</el-button
      >
      <el-button size="medium" type="info" @click="reSet()">重置</el-button>
    </div>
    <el-table
      header-row-class-name="tableHeader"
      :data="tableData"
      border
      style="width: 98%; margin-left: 12px"
    >
      <el-table-column prop="Job_name" label="职位" width="150px">
      </el-table-column>
      <el-table-column prop="user_name" label="投递人" width="100px">
      </el-table-column>

      <el-table-column prop="created_date" label="投递日期" width="150px">
      </el-table-column>
      <el-table-column prop="status" label="投递状态" width="80px">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0" style="color: red">待回复</span>
          <span v-if="scope.row.status == 1" style="color: rgb(65, 151, 46)">已回复</span>
        </template>
      </el-table-column>
      <el-table-column prop="reply" label="我的回复"> </el-table-column>
      <el-table-column prop="empty" label="操作" width="180px">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == 0" size="mini" type="primary" @click="ReplyOpen(scope.row.id)"
            >回复</el-button
          >
          <el-button
            @click="getStudentInfo(scope.row.student_id)"
            size="mini"
            type="info"
            >查看资料</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 信息弹窗 -->
    <el-dialog
      title="学生信息"
      :visible.sync="dialogVisible"
      width="35%"
      :before-close="handleClose"
    >
      <div class="dialogblock-info">
        <div class="title">{{ dialogItem.name }}</div>
        <div class="infoline">学生性别：{{ dialogItem.Sex == 0 ? '男' : '女' }}</div>
        <div class="infoline">入学年份：{{ dialogItem.year }}</div>
        <div class="infoline">专业名称：{{ dialogItem.major }}</div>
        <div class="infoline">平均成绩：{{ dialogItem.score }}</div>
        <div class="infoline">平均绩点：{{ dialogItem.gpa }}</div>
        <div class="infoline">求职方向：{{ dialogItem.direction }}</div>
        <div class="infoline">擅长技能：{{ dialogItem.skill }}</div>
        <div class="infoline">个人简介：{{ dialogItem.introduction }}</div>
        <div class="infoline">联系方式：{{ dialogItem.userPhone }}</div>
        <div v-if="dialogItem.resume" @click="handlePictureCardPreview(dialogItem.resume)" class="infoline" style="color:rgb(65, 151, 46)">查看简历图片</div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
    <!-- 回复弹窗 -->
    <el-dialog
      title="企业回复"
      :visible.sync="dialogReply"
      width="35%"
      :before-close="ReplyClose"
    >
      <div class="dialogblock-info" style="height:200px;">
        <div class="infoline">
          <el-select v-model="replyParam.type" size="medium" style="width: 90%">
          <el-option label="模板回复" :value="0"> </el-option>
          <el-option label="自定义回复" :value="1"> </el-option>
        </el-select>
        </div>
        <div class="infoline">
          <el-select v-if="replyParam.type==0" v-model="replyParam.reply" size="medium" style="width: 90%">
            <el-option v-for=" (item,index) in replyOption" :key="index" :label="item" :value="item"> </el-option>
          </el-select>
        </div>
        <div class="infoline">
          <el-input v-if="replyParam.type==1" v-model="replyParam.reply" size="medium" style="width: 90%"></el-input>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="dialogReply = false">关闭</el-button>
        <el-button size="medium" type="primary" @click="sendReply(replyParam)"
          >回复</el-button
        >

      </span>
    </el-dialog>
    <!-- 简历弹窗 -->
    <el-dialog :visible.sync="ImageVisible">
          <img width="100%" :src="ImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import { allRecord , setRecord } from "@/api/record";
import { localData } from "@/util/local";
import { dateFormat } from "@/util/common";
import { allStudent } from "@/api/student";
import { replyOption } from "@/util/selectOption";

export default {
  data() {
    return {
      ImageVisible:false,
      ImageUrl:"",
      dialogReply: false,
      dialogVisible: false,
      tableData: [],
      initTableData: [],
      userInfo: {},
      replyOption:replyOption,
      search: {
        Job_name: "",
        status: "",
        user_name: "",
      },
      dialogItem: {
        name: "",
        year: "",
        major: "",
        score: "",
        gpa: "",
        direction: "",
        skill: "",
        introduction: "",
      },
      replyParam: {
        type:0,
        id: "",
        reply: "",
      },
    };
  },
  created() {
    this.userInfo = localData("get", "userinfo");
  },
  mounted() {
    this.getMyRecord();
  },
  methods: {
    getMyRecord() {
      const param = {
        company_id: this.userInfo.company_id,
      };
      allRecord(param).then((res) => {
        this.initTableData = res.data.Records.map((item) => {
          item.created_date = dateFormat(
            "YYYY-mm-dd HH:MM",
            new Date(item.created_date)
          );
          item.updated_date = dateFormat(
            "YYYY-mm-dd HH:MM",
            new Date(item.updated_date)
          );
          return item;
        });
        this.tableData = this.initTableData;
        this.searchFromList(this.search);
      });
    },
    searchFromList(search) {
      const { Job_name, status, user_name } = search;
      this.tableData = this.initTableData.filter((item) => {
        return (
          item.Job_name.indexOf(Job_name) !== -1 &&
          item.user_name.indexOf(user_name) !== -1 &&
          item.status.toString().indexOf(status.toString()) !== -1
        );
      });
    },
    reSet() {
      this.search = {
        name: "",
        status,
      };
      this.tableData = this.initTableData;
    },
    getStudentInfo(id) {
      const param = {
        id: id,
      };
      allStudent(param)
        .then((res) => {
          if (res.code == 200) {
            this.dialogItem = { ...res.data.Students[0] };
            this.dialogVisible = true;
          }
        })
        .catch((err) => {
          this.$message({
            showClose: true,
            message: err.data,
            type: "warning",
          });
        });
    },
    ReplyOpen(id) {
      this.replyParam = {
        type:0,
        id: id,
        reply: "",
      };
      this.dialogReply = true;
    },
    sendReply(replyParam){
      this.dialogReply = false
      const { id , reply } = replyParam
      const param = { id , reply ,status:1 } 
      setRecord(param).then(res =>{
        if(res.code == 200){
          this.$message({
            message: "回复成功",
            type: "success",
          });
          this.getMyRecord();
        }
      }).catch(err =>{
        this.$message({
          showClose: true,
          message: err.data,
          type: "warning",
        });
      })

    },
    handleClose() {
      this.dialogVisible = false;
      this.dialogItem = {
        name: "",
        year: "",
        major: "",
        score: "",
        gpa: "",
        direction: "",
        skill: "",
        introduction: "",
      };
    },
    ReplyClose() {
      this.dialogReply = false;
      this.dialogItem = {
        type:0,
        id: "",
        reply: "",
      };
    },
    handlePictureCardPreview(url){
      this.ImageUrl = url
      this.ImageVisible = true
    }
  },
};
</script>

<style>
.tableHeader {
  color: rgb(65, 151, 46) !important;
}
.dialogblock-info {
  width: 100%;
  height: 60vh;
  overflow-y: auto;
}
.dialogblock-info .infoline {
  margin: 10px 0;
  font-size: 18px;
}
.dialogblock-info .title {
  margin: 15px 0;
  font-size: 24px;
  color: rgb(65, 151, 46);
  font-weight: bold;
}
</style>